//
//  LearnInfoViewController.m
//  LiuHeBao
//
//  Created by zr on 2018/8/22.
//  Copyright © 2018年 zr. All rights reserved.
//

#import "LearnInfoViewController.h"

@interface LearnInfoViewController ()

@property (nonatomic, weak)UILabel * titleLabel;
@property (nonatomic, weak)UILabel * timeLabel;
@property (nonatomic, weak)UILabel * readLabel;
@property (nonatomic, weak)UIView * lineView;
@property (nonatomic, weak)UIImageView * contentImageView;
@property (nonatomic, weak)UITextView * textView;

@end

@implementation LearnInfoViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    
    self.view.backgroundColor = WHITE_COLOR;
    
    [self setupNav];
    [self setupUI];
}

- (void)setupNav{
    
    self.title = self.titleStr;
    
    UIButton * backButton = [[UIButton alloc] init];
    
    backButton.frame = CGRectMake(0, 0, 12 * SCREENH6, 20 * SCREENH6);
    [backButton setImage:[UIImage imageNamed:@"1返回"] forState:UIControlStateNormal];
    [backButton addTarget:self action:@selector(backToView) forControlEvents:UIControlEventTouchUpInside];
    
    UIBarButtonItem * leftButton = [[UIBarButtonItem alloc] initWithCustomView:backButton];
    
    self.navigationItem.leftBarButtonItem = leftButton;
    
}


- (void)setupUI{
    
    UILabel * titleLabel = [UILabel new];
    
    titleLabel.textColor = BLACK_COLOR;
    titleLabel.font = [UIFont systemFontOfSize:18 * SCREENH6];
    titleLabel.numberOfLines = 2;
    titleLabel.text = self.model.title;
    
    self.titleLabel = titleLabel;
    
    [self.view addSubview:self.titleLabel];
    
    
    UILabel * timeLabel = [UILabel new];
    
    timeLabel.textColor = ColorWithRGB(0xbebebe);
    timeLabel.font = [UIFont systemFontOfSize:14 * SCREENH6];
    // timeStampString 是服务器返回的13位时间戳
    NSString *timeStampString  = self.model.ctime;
    
    // iOS 生成的时间戳是10位
    NSTimeInterval interval    =[timeStampString doubleValue];
    NSDate *date               = [NSDate dateWithTimeIntervalSince1970:interval];
    
    NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
    [formatter setDateFormat:@"yyyy-MM-dd"];
    NSString *dateString       = [formatter stringFromDate: date];

    timeLabel.text = dateString;
    
    self.timeLabel = timeLabel;
    
    [self.view addSubview:self.timeLabel];
    
    
    UILabel * readLabel = [UILabel new];
    
    readLabel.textColor = ColorWithRGB(0xbebebe);
    readLabel.font = [UIFont systemFontOfSize:14 * SCREENH6];
    readLabel.text = [NSString stringWithFormat:@"%@次阅读",self.model.read];
    
    self.readLabel = readLabel;
    
    [self.view addSubview:self.readLabel];
    
    
    UIView * lineView = [UIView new];
    
    lineView.backgroundColor = ColorWithRGB(0xe6e6e6);//[UIColor lightGrayColor];
    
    self.lineView = lineView;
    
    [self.view addSubview:self.lineView];
    
    
    UIImageView * contentImageView = [UIImageView new];

    NSURL * url = [NSURL URLWithString:self.model.images];
    
    [contentImageView sd_setImageWithURL:url];
    
    self.contentImageView = contentImageView;
    
    [self.view addSubview:self.contentImageView];
    
    
    UITextView *textView =[[UITextView alloc] init];
    textView.backgroundColor= WHITE_COLOR;
    textView.text = self.model.content;
    textView.textColor = [UIColor blackColor];
    textView.editable = NO;
    textView.font = [UIFont systemFontOfSize:15 * SCREENH6];
    
    self.textView = textView;
    
    [self.view addSubview:self.textView];
    
}


- (void)viewDidLayoutSubviews{
    
    [self.titleLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(self.view).offset(10 * SCREENH6);
        make.leading.mas_equalTo(self.view).offset(15 * SCREENH6);
        make.trailing.mas_equalTo(self.view).offset(-15 * SCREENH6);
    }];
    
    [self.timeLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.leading.mas_equalTo(self.titleLabel);
        make.top.mas_equalTo(self.titleLabel.mas_bottom).offset(10 * SCREENH6);
    }];
    
    [self.readLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.trailing.mas_equalTo(self.titleLabel);
        make.centerY.mas_equalTo(self.timeLabel);
    }];
    
    [self.lineView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.leading.trailing.mas_equalTo(self.view);
        make.top.mas_equalTo(self.readLabel.mas_bottom).offset(10 * SCREENH6);
        make.height.mas_equalTo(1 * SCREENH6);
    }];
    
    [self.contentImageView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.leading.trailing.mas_equalTo(self.titleLabel);
        make.top.mas_equalTo(self.lineView.mas_bottom).offset(15 * SCREENH6);
        make.height.mas_equalTo(195 * SCREENH6);
    }];
    
    [self.textView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.leading.trailing.mas_equalTo(self.contentImageView);
        make.top.mas_equalTo(self.contentImageView.mas_bottom).offset(5 * SCREENH6);
        make.bottom.mas_equalTo(self.view);
    }];
}

- (void)backToView {
    
    [self.navigationController popViewControllerAnimated:YES];
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
